/***
author: sailing
date: 2016/12/4
fun: 设置 设备的位置
***/
const maxLen = 16
export default {
  posi (data) {
    var nodes = data.nodes
    console.log(nodes.length)
    var j = 0
    var lineArr = []
    for (var i=6; i > 0; i--) {
      var allArr = this.getType(nodes, i)
      if (allArr.length > 0) {
        this.splitArr(lineArr, allArr)
      }
    }
    this.drawList(lineArr)
  },
  splitArr (lineArr, arr) {
    var len = parseInt(arr.length/maxLen)
    if (arr.length <= maxLen) {
      lineArr.push(arr)
      return
    }
    if (arr.length%maxLen > 0) {
      len++
    }
    
    for (var i=0; i < len; i++) {
      var d = arr.slice(i*maxLen, maxLen*(i+1))
      console.log('dddd start:', d)
      lineArr.push(d)
    }
  },

  drawList (arr) {
    for (var i=0; i<arr.length; i++) {
      this.set(i + 1, arr[i])
    }
  },
  set (h, arr) {
    var y = h * 150
    if (h === 1) y = 100
    for (var i=0; i<arr.length; i++) {
      var lev = 0
      if (i%2) {
        lev = -20
      }
      var position = {
        x: 50 + i*120,
        y: y + lev
      }
      arr[i]['position'] = position
    }
  },
  getType (nodes, type) {
    var arr = []
    for (var i=0; i<nodes.length; i++) {
      var nodeType = nodes[i]['data']['layer']
      if (nodeType === type) {
        arr.push(nodes[i])
      }
    }
    return arr   
  }
}
